package com.xpec.c4.service.common;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.net.XSENetSession;
import com.xpec.c4.service.server.Server;

/**
 * 
 * 
 * @author Bany
 * 
 * @since 2008-8-30 下午09:26:52
 * 
 */

public class ServiceInfo {

	/**
	 * 服务列表
	 */
	private static Map<Integer, Server> servers = Collections
			.synchronizedMap(new HashMap<Integer, Server>());

	/**
	 * 连接的Session列表<br>
	 * key为session address<br>
	 * value为session实体
	 */
	private static Map<GUID, XSENetSession> sessions = Collections.synchronizedMap(new HashMap<GUID, XSENetSession>());

	/**
	 * 连接的Session对应的socket地址.<br>
	 * key为session hashcode<br>
	 * value为session address
	 */
	private static Map<XSENetSession, GUID> sessionsAddress = Collections.synchronizedMap(new HashMap<XSENetSession, GUID>());
	
	private static int model = ServerModel.RUN;

	/**
	 * 得到全部的服务器集合
	 * @return
	 */
	public static Collection<Server> getAllServer() {
		return servers.values();
	}
	
	/**
	 * 全部的服务器映射表
	 * @return
	 */
	public static Map<Integer, Server> getServers() {
		return servers;
	}

	/**
	 * 设置服务器的集合
	 * @param serverMap
	 */
	public static void setServers(Map<Integer, Server> serverMap) {
		servers = serverMap;
	}

	/**
	 * 添加服务器
	 * @param server
	 */
	public static void addServer(Server server) {
		servers.put(server.getPort(), server);
	}
	
	/**
	 * 获取服务器
	 * @param server
	 */
	public static Server getServer(int serverPort) {
		return servers.get(serverPort);
	}

	/**
	 * 获取全部的session集合
	 * @return
	 */
	public static Map<GUID, XSENetSession> getSessions() {
		return sessions;
	}

	/**
	 * 获取session數量
	 * @return
	 */
	public static int getSessionCount() {
		return sessions.size();
	}
	
	/**
	 * 设置sessions
	 * @param sessions
	 */
	public static void setSessions(Map<GUID, XSENetSession> sessions) {
		ServiceInfo.sessions = sessions;
	}
	
	/**
	 * 获取全部连接的session
	 * @param port
	 * @return
	 */
	public static Collection<XSENetSession> getAllSession() {
		return sessions.values();
	}

	/**
	 * 添加session
	 * @param session
	 */
	public static void addSession(GUID key, XSENetSession session) {
		sessions.put(key, session);
		sessionsAddress.put(session, key);
	}

	/**
	 * 通过地址，获得session
	 * @param remoteAddress
	 * @return
	 */
	public static XSENetSession getSession(GUID key) 
	{
		return sessions.get(key);
	}
	
	/**
	 * 获取全部的session ID
	 * @return
	 */
	public static Collection<GUID> getAllSessionID() 
	{
		return sessions.keySet();
	}

	/**
	 * 移除session，通过远程地址
	 * @param remoteAddress 远程地址
	 */
	public static void removeSession(GUID key) 
	{
		XSENetSession session = sessions.remove(key);
		sessionsAddress.remove(session);
	}

	/**
	 * 移除session，通过session
	 * @param session session
	 */
	public static void removeSession(XSENetSession session) 
	{
		GUID key = sessionsAddress.get(session);
		sessions.remove(key);
		sessionsAddress.remove(session);		
	}
	
	/**
	 * 获取session的地址
	 * @param session
	 * @return
	 */
	public static GUID getSessionAddress(XSENetSession session)
	{
		return sessionsAddress.get(session);
	}

	/**
	 * 运行模式
	 * 
	 */
	public static void runModel() 
	{
		model = ServerModel.RUN;
	}
	
	/**
	 * 测试用例模式
	 * 
	 */
	public static void testcaseModel()
	{
		model = ServerModel.TESTCASE;
	}

	/**
	 * 获得服务器模式
	 * 
	 * @return
	 */
	public static int getModel() 
	{
		return model;
	}
}
